﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

/**********************************
 * 
 * 公司：中软强网
 * 编写人：陈学锋
 * 日期：2008年11月12日
 * 
 * ********************************/
namespace DeviceTest1
{
   public partial class MainForm : Form
    {
        private string TemStr = "";//TEMP字符        
        private string queryChar = "";//查找字符
       private int findSpace = 0;//查找位置

       SetFont setFontForm;

        public MainForm()
        {
            InitializeComponent();
            textBox1.Enabled = false;
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///创建记事 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox1.Enabled = true;
        }

        /// <summary>
        ///保存记事 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem8_Click(object sender, EventArgs e)
        {
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|(*.*)";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
                    writer.Write(textBox1.Text);
                    writer.Close();
                    MessageBox.Show("保存成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
        }

        /// <summary>
        /// 打开记事
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem9_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|(*.*)";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    FileInfo f = new FileInfo(openFileDialog.FileName);
                    StreamReader reader = f.OpenText();
                    textBox1.Text = reader.ReadToEnd();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            textBox1.Select(0, textBox1.TextLength);
            TemStr = textBox1.SelectedText;
        }
        /// <summary>
        /// 粘贴操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem6_Click(object sender, EventArgs e)
        {
            //textBox1.Text = textBox1.Text + TemStr;
            int strLength=textBox1.SelectedText.Length;//获取选中字符的长度
            int strStart = textBox1.SelectionStart;//获取选中字符的起始点
            string str=textBox1.Text.Insert(strStart,TemStr);
            textBox1.Text = str;
            textBox1.SelectionStart = strStart + TemStr.Length;
        }
        /// <summary>
        /// 退出操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem22_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 另存为操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem24_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.FileName == null)
                return;
            else
            {

                saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|(*.*)";

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
                        writer.Write(textBox1.Text);
                        writer.Close();
                        MessageBox.Show("保存成功！");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem7_Click(object sender, EventArgs e)
        {
            //int strLength=textBox1.SelectedText.Length;//获取选中字符的长度
            //int strStart = textBox1.SelectionStart;//获取选中字符的起始点
            //string str = "选中字符的开始位置：" + strStart + "  " + strLength;
            //MessageBox.Show(str);
            string str1 = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectedText.Length);
            textBox1.Text = str1;
            textBox1.SelectionStart = textBox1.Text.Length;
        }
        /// <summary>
        /// 剪切操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem4_Click_1(object sender, EventArgs e)
        {
            int strLength = textBox1.SelectedText.Length;//获取选中字符的长度
            int strStart = textBox1.SelectionStart;//获取选中字符的起始点

            TemStr=textBox1.SelectedText;//选中的字符
            string str= textBox1.Text.Remove(strStart, strLength);
            textBox1.Text =str;
            textBox1.SelectionStart = strStart;
            
            textBox1.Update();
        }
        /// <summary>
        /// 复制操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem5_Click(object sender, EventArgs e)
        {
            TemStr = textBox1.SelectedText;//选中的字符            
        }
       /// <summary>
       /// 撤消操作
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem12_Click(object sender, EventArgs e)
       {
           textBox1.Undo();
       }
       /// <summary>
       /// 全选操作
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem23_Click(object sender, EventArgs e)
       {
           textBox1.SelectAll();
       }
       /// <summary>
       /// MainMenu查找操作
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem13_Click(object sender, EventArgs e)
       {
           
       }
       /// <summary>
       /// 日期/时间
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem16_Click(object sender, EventArgs e)
       {
           textBox1.SelectedText = DateTime.Now.ToString();
       }
       /// <summary>
       /// 状态栏
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem20_Click(object sender, EventArgs e)
       {
           
           if (menuItem20.Checked == true)
           {
               statusBar1.Visible = false;
               menuItem20.Checked = false;
           }
           else
           {
               statusBar1.Visible = true;
               menuItem20.Checked = true;
           }           
       }
       /// <summary>
       /// 帮助操作
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem25_Click(object sender, EventArgs e)
       {
           HelpForm helpForm = new HelpForm();
           helpForm.Show();
           this.Hide();
       }
       /// <summary>
       /// 关于操作
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem26_Click(object sender, EventArgs e)
       {
           About about = new About();
           about.Show();
           this.Hide();
       }
       /// <summary>
       /// 自动换行
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem18_Click(object sender, EventArgs e)
       {
           if (menuItem18.Checked == true)
           {
               menuItem18.Checked = false;
               textBox1.ScrollBars = ScrollBars.Horizontal;
               textBox1.WordWrap = false;
           }
           else
           {
               menuItem18.Checked = true;
               textBox1.ScrollBars = ScrollBars.Vertical;
               textBox1.WordWrap = true;
           }
           
       }
       /// <summary>
       /// textBox中的文本改变时发生事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void textBox1_TextChanged(object sender, EventArgs e)
       {
           if (textBox1.TextLength==0)
           {   
               menuItem8.Enabled=false;//保存
               menuItem24.Enabled=false;//另存为
               menuItem12.Enabled=false;//撤消
               menuItem6.Enabled=false;//粘贴
               menuItem7.Enabled=false;//删除
               menuItem4.Enabled=false;//剪切
               menuItem5.Enabled = false;//复制
           }
           else
           {

               menuItem8.Enabled = true;//保存
               menuItem24.Enabled = true;//另存为
               menuItem12.Enabled = true;//撤消
               menuItem6.Enabled = true;//粘贴
               menuItem7.Enabled = true;//删除
               menuItem4.Enabled = true;//剪切
               menuItem5.Enabled = true;//复制
           }

           if (menuItem20.Enabled == true)
           {
               if (menuItem20.Checked == true)
               {
                   status(sender, e);
               }
               else
               {
                   statusBar1.Text = "";
               }
           }
       }
       /// <summary>
       /// 状态栏记录
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void status(object sender, EventArgs e)
       {           
           statusBar1.Text = "您已经输入" + textBox1.TextLength.ToString() + "字符        位置：第" + textBox1.SelectionStart.ToString() + "个字符";          
       }

       private void textBox1_KeyUp(object sender, KeyEventArgs e)
       {
           status(sender, e);
       }
       /// <summary>
       /// 窗体加载时
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void MainForm_Load(object sender, EventArgs e)
       {
           menuItem8.Enabled = false;//保存
           menuItem24.Enabled = false;//另存为
           menuItem12.Enabled = false;//撤消
           menuItem6.Enabled = false;//粘贴
           menuItem7.Enabled = false;//删除
           menuItem4.Enabled = false;//剪切
           menuItem5.Enabled = false;//复制

           menuItem20.Checked = true;//状态栏
       }
       /// <summary>
       /// MainMenu查找下一个
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem14_Click(object sender, EventArgs e)
       {

       }
       /// <summary>
       /// MainMenu替换
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem15_Click(object sender, EventArgs e)
       {

       }
       /// <summary>
       /// 查找
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem27_Click(object sender, EventArgs e)
       {
           textBox1.Focus();          
           findSpace = textBox1.Text.IndexOf(textBox2.Text,0);           
           textBox1.Select(findSpace, textBox2.TextLength);//选中查找的内容
          
       }
       /// <summary>
       /// 查找上一处
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem30_Click(object sender, EventArgs e)
       {
           textBox1.Focus();          

           if ((findSpace = textBox1.Text.LastIndexOf(textBox2.Text, findSpace)) == -1)
           {
               MessageBox.Show("向上查找完毕！");
               findSpace = 0;
           }
           else
           {
               textBox1.Select(findSpace, textBox2.TextLength);
               findSpace = findSpace - textBox2.TextLength;
           }          
       }
       /// <summary>
       /// 查找下一处
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem28_Click(object sender, EventArgs e)
       {
           textBox1.Focus();
           if ((findSpace = textBox1.Text.IndexOf(textBox2.Text, findSpace)) == -1)
           {
               MessageBox.Show("查找完毕！");
               findSpace = textBox1.TextLength;
           }
           else
           {
               textBox1.Select(findSpace, textBox2.TextLength);
               findSpace = findSpace + textBox2.TextLength;              
           }
       }
       /// <summary>
       /// 替换
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem29_Click(object sender, EventArgs e)
       {
           textBox1.Focus();
           queryChar = textBox2.Text;
           findSpace = textBox1.Text.IndexOf(textBox2.Text,findSpace);
           if (findSpace == -1)
           {
               findSpace = 0;
               return;
           }
           textBox1.Select(findSpace, queryChar.Length);
           
           string str1 = textBox3.Text;
           string str2 = textBox1.SelectedText;
           if (str2 != "")
           {
               string str3 = textBox1.SelectedText.Replace(str2,str1);               
               textBox1.SelectedText = str3;               
           }
       }
       /// <summary>
       /// 全部替换
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem31_Click(object sender, EventArgs e)
       {
           textBox1.Focus();
           queryChar = textBox2.Text;
           findSpace = textBox1.TextLength;

           if (textBox1.TextLength > 0)
           {
               for (; ; )
               {
                   if ((findSpace = textBox1.Text.LastIndexOf(textBox2.Text, findSpace)) == -1)
                   {
                       findSpace = 0;
                       break;
                   }
                   else
                   {
                       textBox1.Select(findSpace,queryChar.Length);
                       string str1=textBox3.Text;
                       string str2=textBox1.SelectedText;
                       if (str2 != "")
                       {
                           string str3 = textBox1.SelectedText.Replace(str2, str1);
                           textBox1.SelectedText = str3;
                       }
                   }
               }
           }
          
       }
       /// <summary>
       /// 字体设置
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void menuItem19_Click(object sender, EventArgs e)
       {          
          setFontForm = new SetFont();
          setFontForm.Show();
          this.Hide();

          /*textBox1.Font = setFontForm.SetFontFa1;
          
          if (setFontForm.SetFontColor.Trim() == "黑色")
          {
              textBox1.ForeColor = Color.Black;
              MessageBox.Show("黑色");
          }
          if (setFontForm.SetFontColor.Trim() == "红色")
          {
              textBox1.ForeColor = Color.Red;
              MessageBox.Show("红色");
          }
          if (setFontForm.SetFontColor.Trim() == "绿色")
          {
              textBox1.ForeColor = Color.Green;
              MessageBox.Show("绿色");
          }
          //this.Hide();*/
           
       }
    }
}